<template>
  <section class="component manual-distrubute">
    <el-form :model="manualModel" ref="form" label-width="90px" class="tableInput">
      <el-form-item>
        <span style="color:#20a0ff;">当前已选择{{count}}个案件，案件总金额￥{{sum | toThousands}}</span>
      </el-form-item>
      <el-form-item prop="distributeType">
        <el-radio-group v-model="manualModel.distributeType" @change="getCollectionDays">
          <el-radio label="TEL" v-if="!visabledPool.includes('TEL')">分配到电催案件池</el-radio>
          <el-radio label="VISIT" v-if="!visabledPool.includes('VISIT')">分配到外访案件池</el-radio>
          <el-radio label="LEGAL" v-if="!visabledPool.includes('LEGAL')">分配到法务案件池</el-radio>
          <!-- <el-radio label="OUTER" v-if="!visabledPool.includes('OUTER')">分配到委外案件池</el-radio> -->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="催收天数" prop="partCollectionDays">
        <el-input v-model.trim="manualModel.partCollectionDays" type="number"></el-input>
      </el-form-item>
    </el-form>
    <div class="operate-buttons">
      <el-button @click="close">取消</el-button>
      <el-button @click="submit" :loading="submitLoading">确定</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Dependencies } from '~/core/decorator';
import { State } from 'vuex-class';
import { Emit, Prop } from 'vue-property-decorator';
import { PageService } from '~/utils/page.service';

import { CaseInfoService } from '../../services/domain-service/case-info.service';
import { dataimpService } from "~/config/server";
import el from "element-ui/src/locale/lang/el";

@Component({
  components: {},
})
export default class ManualDistrubute extends Vue {
  private sum: any = 0;
  private days: any = 0;
  private count: any = 0;
  @Prop()
  private selectionList: any;

  @Prop({ default: () => [] })
  private visabledPool: string[];

  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(CaseInfoService)
  private CaseInfoService: CaseInfoService;
  private caseList: any = []; // 案件数组
  private manualModel: any = {
    caseIds: '',
    distributeType: 'TEL',
    partCollectionDays: '',
  };
  @Prop()
  private importModel: any;
  private submitLoading: boolean = false;


  @Emit('refreshList')
  public refreshList() {
  }

  @Emit('close')
  public close() {
  }

  private reset() {
    const form: any = this.$refs['form']
    form.resetFields();
  }


  private created() {
    this.overdueAmount();
    this.caseCount();
    this.getCollectionDays(this.manualModel.distributeType);
  }

  private getCollectionDays(val) {
    this.CaseInfoService.getCollectionDays(this.manualModel.distributeType).subscribe(
      (data) => {
        if (data) {
          this.manualModel.partCollectionDays = data;
          this.days = data;
        }
      });
  }

  private overdueAmount() {
    const paramDatas: any = {
      principalId: this.importModel.principalId,
      batchNumber: this.importModel.batchNumber,
      mobileNo: this.importModel.mobileNo,
      personalName: this.importModel.personalName,
      dayRange: this.importModel.dayRange,
      amountRange: this.importModel.amountRange,
      rateRange: this.importModel.rateRange,
      handNumber: this.importModel.handNumber,
      overduePeriod: this.importModel.overduePeriod,
      areadId: this.importModel.areadId,
      labelName: this.importModel.labelName,
      distributeType: this.importModel.distributeType,
      partCollectionDays: this.importModel.partCollectionDays,
      caseNumber: this.importModel.caseNumber,
      principalName: this.importModel.principalName,
      partnerName: this.importModel.partnerName,
      bankName: this.importModel.bankName,
      businessAddress: this.importModel.businessAddress,
      boardAddress: this.importModel.boardAddress,
      carNo: this.importModel.carNo,
      idCard: this.importModel.idCard,
      overdueAmountStart: this.importModel.amountRange ? this.importModel.amountRange.min : '',
      overdueAmountEnd: this.importModel.amountRange ? this.importModel.amountRange.max : '',
      dataFromSource: this.importModel.dataFromSource ? this.importModel.dataFromSource : null,
      loanDateMin: this.$filter.dateRanageFormat(this.importModel.loanDate).start,
      loanDateMax: this.$filter.dateRanageFormat(this.importModel.loanDate).end
    };
    if (this.selectionList.length === 0) {
      this.CaseInfoService.getOverdueAmount(paramDatas).subscribe(
      (data) => {
        this.setSum(data);
      });
    }else{
      this.sum = this.selectionList
        .map((v) => v.overdueAmount)
        .reduce((a, b) => a + b, 0)
        .toFixed(2);
    }
  }
  private setSum(sum) {
    if (this.selectionList.length === 0) {
      this.sum = sum;
      return;
    } else {
      this.sum = this.selectionList
        .map((v) => v.overdueAmount)
        .reduce((a, b) => a + b, 0)
        .toFixed(2);
    }
  }
  private caseCount() {
    const paramDatas: any = {
      principalId: this.importModel.principalId,
      batchNumber: this.importModel.batchNumber,
      mobileNo: this.importModel.mobileNo,
      personalName: this.importModel.personalName,
      dayRange: this.importModel.dayRange,
      amountRange: this.importModel.amountRange,
      rateRange: this.importModel.rateRange,
      handNumber: this.importModel.handNumber,
      overduePeriod: this.importModel.overduePeriod,
      areadId: this.importModel.areadId,
      labelName: this.importModel.labelName,
      distributeType: this.importModel.distributeType,
      partCollectionDays: this.importModel.partCollectionDays,
      caseNumber: this.importModel.caseNumber,
      principalName: this.importModel.principalName,
      partnerName: this.importModel.partnerName,
      bankName: this.importModel.bankName,
      businessAddress: this.importModel.businessAddress,
      boardAddress: this.importModel.boardAddress,
      carNo: this.importModel.carNo,
      idCard: this.importModel.idCard,
      overdueAmountStart: this.importModel.amountRange ? this.importModel.amountRange.min : '',
      overdueAmountEnd: this.importModel.amountRange ? this.importModel.amountRange.max : '',
      dataFromSource: this.importModel.dataFromSource ? this.importModel.dataFromSource : null,
      loanDateMin: this.$filter.dateRanageFormat(this.importModel.loanDate).start,
      loanDateMax: this.$filter.dateRanageFormat(this.importModel.loanDate).end
    };
    if (this.selectionList.length === 0) {
      this.CaseInfoService.getCaseCount(paramDatas).subscribe((data) => {
        this.setCount(data)
      });
    }else{
      this.count = this.selectionList.length
    }
  }

  private setCount(count) {
    if (this.selectionList.length === 0) {
      this.count = count;
      return;
    }
    this.count = this.selectionList.length
  }

  /**
   * 刷新
   */
  private refresh(caseList, importModel) {
    this.overdueAmount();
    this.caseCount();
    this.importModel = importModel;
    this.caseList = caseList;
  }

  /**
   * 确定
   */
  private submit() {
    if(this.count && this.count > 200){
      this.$message.error('分案案件数量不能超过200！');
      return;
    }
    if (!this.manualModel.partCollectionDays) {
      this.$message.error('请输入催收天数！');
      return;
    }
    if (this.days && this.days < this.manualModel.partCollectionDays) {
      this.$message.error('催收天数不能大于' + this.days);
      return;
    }
    if (this.selectionList.length === 0) {
      const paramDatas: any = {
        principalId: this.importModel.principalId,
        batchNumber: this.importModel.batchNumber,
        mobileNo: this.importModel.mobileNo,
        personalName: this.importModel.personalName,
        dayRange: this.importModel.dayRange,
        amountRange: this.importModel.amountRange,
        rateRange: this.importModel.rateRange,
        handNumber: this.importModel.handNumber,
        overduePeriod: this.importModel.overduePeriod,
        areadId: this.importModel.areadId,
        labelName: this.importModel.labelName,
        distributeType: this.manualModel.distributeType,
        partCollectionDays: this.manualModel.partCollectionDays,
        caseNumber: this.importModel.caseNumber,
        principalName: this.importModel.principalName,
        partnerName: this.importModel.partnerName,
        bankName: this.importModel.bankName,
        businessAddress: this.importModel.businessAddress,
        boardAddress: this.importModel.boardAddress,
        carNo: this.importModel.carNo,
        idCard: this.importModel.idCard,
        overdueAmountStart: this.importModel.amountRange ? this.importModel.amountRange.min : '',
        overdueAmountEnd: this.importModel.amountRange ? this.importModel.amountRange.max : '',
        dataFromSource: this.importModel.dataFromSource ? this.importModel.dataFromSource : null,
        loanDateMin: this.$filter.dateRanageFormat(this.importModel.loanDate).start,
        loanDateMax: this.$filter.dateRanageFormat(this.importModel.loanDate).end
      };
      this.submitLoading = true;
      this.CaseInfoService.distributeCaseInfoByRequest(paramDatas).subscribe(
        (data) => {
          this.submitLoading = false;
          this.$message.success('分配成功');
          this.refreshList();
          this.close();
        },
        ({ msg }) => {
          this.submitLoading = false;
        },
      );
    } else {
      const caseId: any = this.selectionList.map((v) => v.id);
      const paramData: any = {
        caseIds: caseId,
        distributeType: this.manualModel.distributeType,
        partCollectionDays: this.manualModel.partCollectionDays,
      };
      this.submitLoading = true;
      this.CaseInfoService.distributeCaseInfo(paramData).subscribe(
        (data) => {
          this.submitLoading = false;
          this.$message.success('分配成功');
          this.refreshList();
          this.close();
        },
        ({ msg }) => {
          this.submitLoading = false;
        },
      );
    }
  }
}
</script>

<style lang="less" scoped>
</style>
